package com.ctu.study
{
	import flash.text.engine.*;
	
	import mx.core.UIComponent;

	public class TextElement_replaceTextExample extends UIComponent
	{
		public function TextElement_replaceTextExample():void
		{
			var str:String = "0123456 0123456 0123456 0123456 01234560 123456 0123 45601 23456 0123 4560 123456 0123456";
            var fontDescription:FontDescription = new FontDescription("Arial");
            var format:ElementFormat = new ElementFormat(fontDescription);
            format.fontSize = 14;
            var textElement:TextElement = new TextElement(str+str+str+str+str+str+str+str+str+str, format);
            var textBlock:TextBlock = new TextBlock();
            textBlock.content = textElement;
            createLine(textBlock, 10);
            
            textElement.replaceText(500, 501, "H");
//            var elFormat:ElementFormat = textElement.elementFormat.clone();
//            elFormat.fontSize =13;
//            textElement.elementFormat = elFormat;
            createLine(textBlock, 300);
		}
		
		private function createLine(textBlock:TextBlock, y:Number):void {
			var textElement:TextElement = textBlock.content as TextElement;
			var firstInvalidLine:TextLine = textBlock.firstInvalidLine;
			var beginBlock:int = -1;
			if(firstInvalidLine)
				beginBlock = firstInvalidLine.textBlockBeginIndex;
				
			trace("firstInvalidLine:"+beginBlock);
			var tLine:TextLine = textBlock.firstLine;
			var cnt:int = 0;
			while(tLine && tLine.nextLine){
				trace("line["+cnt+"] blockBegin:"+tLine.textBlockBeginIndex+" validity:"+tLine.validity+" text:"+ textElement.text.substr(tLine.textBlockBeginIndex, tLine.rawTextLength));
				tLine = tLine.nextLine;
				cnt++;
			}
			
			if(textBlock.lastLine){
				tLine = textBlock.lastLine;
				trace("line["+cnt+"] blockBegin:"+tLine.textBlockBeginIndex+" validity:"+tLine.validity+" text:"+ textElement.text.substr(tLine.textBlockBeginIndex, tLine.rawTextLength));
			}
			
            var textLine:TextLine = textBlock.createTextLine((firstInvalidLine)?firstInvalidLine.previousLine:null, 600);
            var vPos:Number = y;
            var myCnt:int = 0;
            while(textLine){
	            textLine.x = 0;
	            textLine.y = vPos;
	            addChild(textLine);
	            
	            vPos += textLine.height + 6;
	            if(firstInvalidLine && textLine.validity == TextLineValidity.VALID)
	            {
	            	trace("----- next line validy:"+textLine.nextLine.validity);
	            	break;
	            }
	            textLine = textBlock.createTextLine(textLine,600);
	            myCnt++;
            }
            
            trace("myCnt:"+myCnt+"--------------------------------------------------------------------------------------");
            tLine = textBlock.firstLine;
			cnt = 0;
			while(tLine && tLine.nextLine){
				trace("line["+cnt+"] blockBegin:"+tLine.textBlockBeginIndex+" validity:"+tLine.validity+" text:"+ textElement.text.substr(tLine.textBlockBeginIndex, tLine.rawTextLength));
				tLine = tLine.nextLine;
				cnt++;
			}
			
			if(textBlock.lastLine){
				tLine = textBlock.lastLine;
				trace("line["+cnt+"] blockBegin:"+tLine.textBlockBeginIndex+" validity:"+tLine.validity+" text:"+ textElement.text.substr(tLine.textBlockBeginIndex, tLine.rawTextLength));
			}
			
			trace("----------------//");
        }


	}
}